// group_database.cpp
#include "group_database.h"
#include <QSqlError>
#include <QDebug>
#include <QJsonObject>
GroupDatabase::GroupDatabase(DatabaseConnection *dbConnection, QObject *parent)
    : QObject(parent), dbConnection(dbConnection) {}

int GroupDatabase::createGroup(const QString &groupName, int creatorId) {
    QSqlDatabase db = dbConnection->database();
    QSqlQuery query(db);

    QString sql = "INSERT INTO chat_groups (group_name, creator_id) VALUES (:group_name, :creator_id)";
    query.prepare(sql);
    query.bindValue(":group_name", groupName);
    query.bindValue(":creator_id", creatorId);

    if (!query.exec()) {
        qDebug() << "【群聊数据库】创建群聊失败：" << query.lastError().text();
        return -1;
    }

    int groupId = query.lastInsertId().toInt();

    // 将创建者加入群成员表
    if (!addGroupMember(groupId, creatorId)) {
        qDebug() << "【群聊数据库】将创建者加入群成员表失败，群聊 ID: " << groupId;
        // 可以选择回滚群聊创建操作，这里简单输出错误信息
    }

    return groupId;
}

bool GroupDatabase::addGroupMember(int groupId, int userId) {
    QSqlDatabase db = dbConnection->database();
    QSqlQuery query(db);

    QString sql = "INSERT INTO group_members (group_id, user_id) VALUES (:group_id, :user_id)";
    query.prepare(sql);
    query.bindValue(":group_id", groupId);
    query.bindValue(":user_id", userId);

    if (!query.exec()) {
        qDebug() << "【群聊数据库】添加群成员失败：" << query.lastError().text();
        return false;
    }

    return true;
}

bool GroupDatabase::removeGroupMember(int groupId, int userId) {
    QSqlDatabase db = dbConnection->database();
    QSqlQuery query(db);

    QString sql = "DELETE FROM group_members WHERE group_id = :group_id AND user_id = :user_id";
    query.prepare(sql);
    query.bindValue(":group_id", groupId);
    query.bindValue(":user_id", userId);

    if (!query.exec()) {
        qDebug() << "【群聊数据库】删除群成员失败：" << query.lastError().text();
        return false;
    }

    return true;
}

QList<int> GroupDatabase::getGroupMembers(int groupId) {
    QList<int> memberList;
    QSqlDatabase db = dbConnection->database();
    QSqlQuery query(db);

    QString sql = "SELECT user_id FROM group_members WHERE group_id = :group_id";
    query.prepare(sql);
    query.bindValue(":group_id", groupId);

    if (!query.exec()) {
        qDebug() << "【群聊数据库】获取群成员列表失败：" << query.lastError().text();
        return memberList;
    }

    while (query.next()) {
        memberList.append(query.value(0).toInt());
    }

    return memberList;
}

bool GroupDatabase::sendGroupMessage(int groupId, int senderId, const QString &messageText) {
    QSqlDatabase db = dbConnection->database();
    QSqlQuery query(db);

    QString sql = "INSERT INTO group_messages (group_id, sender_id, message_text) VALUES (:group_id, :sender_id, :message_text)";
    query.prepare(sql);
    query.bindValue(":group_id", groupId);
    query.bindValue(":sender_id", senderId);
    query.bindValue(":message_text", messageText);

    if (!query.exec()) {
        qDebug() << "【群聊数据库】发送群消息失败：" << query.lastError().text();
        return false;
    }

    return true;
}

QList<QJsonObject> GroupDatabase::getGroupMessages(int groupId) {
    QList<QJsonObject> messageList;
    QSqlDatabase db = dbConnection->database();
    QSqlQuery query(db);

    QString sql = "SELECT id, group_id, sender_id, message_text FROM group_messages WHERE group_id = :group_id";
    query.prepare(sql);
    query.bindValue(":group_id", groupId);

    if (!query.exec()) {
        qDebug() << "【群聊数据库】获取群消息列表失败：" << query.lastError().text();
        return messageList;
    }

    while (query.next()) {
        QJsonObject message;
        message["id"] = query.value(0).toInt();
        message["groupId"] = query.value(1).toInt();
        message["senderId"] = query.value(2).toInt();
        message["messageText"] = query.value(3).toString();
        messageList.append(message);
    }

    return messageList;
}
